package com.yulang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yulang.entity.SysRole;
import com.yulang.entity.SysRolePermission;
import com.yulang.mapper.SysRoleMapper;
import com.yulang.mapper.SysRolePermissionMapper;
import com.yulang.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;

    @Transactional
    @Override
    public void saveOrUpdateRole(SysRole sysRole) {
        saveOrUpdate(sysRole);
        // 保存权限数据
        QueryWrapper<SysRolePermission> query = new QueryWrapper<>();
        query.lambda().eq(SysRolePermission::getRoleId,sysRole.getId());
        sysRolePermissionMapper.delete(query);
        sysRole.getPerIds().forEach(item -> {
            SysRolePermission sysRolePermission = new SysRolePermission();
            sysRolePermission.setRoleId(sysRole.getId()+"");
            sysRolePermission.setPermissionId(item+"");
            sysRolePermissionMapper.insert(sysRolePermission);
        });


    }
}
